<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js事件课堂案例</title>
</head>
<body>
    <img id="light" src="../img/off.gif"> <br>
    <input type="button" value="点亮" >
    <input type="button" value="熄灭" >
    <br> <br>
    <input type="text" onfocus="lower()" onblur="upper()" id="name" value="BRIUP">
    <br> <br>
    <input type="checkbox" name="hobby"> 电影
    <input type="checkbox" name="hobby"> 旅游
    <input type="checkbox" name="hobby"> 游戏
    <br>
    <input type="button" value="全选" onclick="checkAll()">
    <input type="button" value="反选" onclick="reverse()">
    <script>
        // 点亮灯泡
        // 获取两个button的元素，修改对应的img的src的值
        var oImg = document.getElementById("light");
        // 获取所有的input取前两个
        var oInputs = document.getElementsByTagName("input");
        var oBtnOn = oInputs[0];
        var oBtnOff = oInputs[1];
        // 绑定事件
        oBtnOn.onclick = function(){
            oImg.src = "../img/on.gif";
        }
        oBtnOff.onclick = function(){
            oImg.src = "../img/off.gif";
        }

        // 输入框获取焦点和失去焦点
        // 获取焦点小写、失去焦点大写
        var oText = document.getElementById("name");
        function lower(){
            oText.value = oText.value.toLowerCase();
        }
        function upper(){
            oText.value = oText.value.toUpperCase();
        }

        // 复选框全选和反选
        // 获取所有的复选框
        var oCheckBox = document.getElementsByName("hobby");
        // 设置其元素的checked属性为true还是false
        function checkAll(){
            oCheckBox.forEach((value)=>{
                value.checked = true;
            });
        };
        function reverse(){
            oCheckBox.forEach((value)=>{
                value.checked = !value.checked;
            });
        };
        
    </script>
</body>
</html>